#ifndef WAREHOUSEVO_H
#define WAREHOUSEVO_H

#include <QObject>
#include <QString>
#include <QDateTime>
#include <QList>
#include "pogo/entity/shelf.h"

class WarehouseVO : public QObject
{
    Q_OBJECT
    // 元数据定义，用于反射和属性访问
    Q_PROPERTY(int warehouseId READ getWarehouseId WRITE setWarehouseId)
    Q_PROPERTY(QString warehouseName READ getWarehouseName WRITE setWarehouseName)
    Q_PROPERTY(int warehouseQuantity READ getWarehouseQuantity WRITE setWarehouseQuantity)
    Q_PROPERTY(QList<Shelf> shelves READ getShelves WRITE setShelves)

public:
    // 默认构造函数
    explicit WarehouseVO(QObject *parent = nullptr)
        : QObject(parent), m_warehouseId(0), m_warehouseQuantity(0) {}

    // 基础属性构造函数
    WarehouseVO(int id, const QString &name, int quantity, QObject *parent = nullptr)
        : QObject(parent), m_warehouseId(id), m_warehouseName(name), m_warehouseQuantity(quantity) {}

    // 带货架列表的构造函数
    WarehouseVO(int id, const QString &name, int quantity,
                const QList<Shelf> &shelves, QObject *parent = nullptr)
        : QObject(parent), m_warehouseId(id), m_warehouseName(name),
          m_warehouseQuantity(quantity), m_shelves(shelves) {}

    // 复制构造函数
    WarehouseVO(const WarehouseVO &other, QObject *parent = nullptr)
        : QObject(parent),
          m_warehouseId(other.m_warehouseId),
          m_warehouseName(other.m_warehouseName),
          m_warehouseQuantity(other.m_warehouseQuantity),
          m_shelves(other.m_shelves) {}

    // 析构函数
    ~WarehouseVO() override = default;

    // 赋值运算符重载
    WarehouseVO &operator=(const WarehouseVO &other) {
        if (this != &other) {
            m_warehouseId = other.m_warehouseId;
            m_warehouseName = other.m_warehouseName;
            m_warehouseQuantity = other.m_warehouseQuantity;
            m_shelves = other.m_shelves;
        }
        return *this;
    }

    // 仓库ID
    int getWarehouseId() const { return m_warehouseId; }
    void setWarehouseId(int id) {
        if (m_warehouseId != id) {
            m_warehouseId = id;
            emit warehouseIdChanged(id);
        }
    }

    // 仓库名称
    QString getWarehouseName() const { return m_warehouseName; }
    void setWarehouseName(const QString &name) {
        if (m_warehouseName != name) {
            m_warehouseName = name;
            emit warehouseNameChanged(name);
        }
    }

    // 仓库物料总数量
    int getWarehouseQuantity() const { return m_warehouseQuantity; }
    void setWarehouseQuantity(int quantity) {
        if (m_warehouseQuantity != quantity) {
            m_warehouseQuantity = quantity;
            emit warehouseQuantityChanged(quantity);
        }
    }

    // 货架列表操作
    QList<Shelf> getShelves() const { return m_shelves; }
    void setShelves(const QList<Shelf> &shelves) {
        m_shelves = shelves;
        emit shelvesChanged();
    }

    // 添加单个货架
    void addShelf(const Shelf &shelf) {
        m_shelves.append(shelf);
        emit shelvesChanged();
    }

    // 移除指定货架
    void removeShelf(int shelfId) {
        for (int i = 0; i < m_shelves.size(); ++i) {
            if (m_shelves[i].get_id() == shelfId) {
                m_shelves.removeAt(i);
                emit shelvesChanged();
                break;
            }
        }
    }

    // 清空货架列表
    void clearShelves() {
        m_shelves.clear();
        emit shelvesChanged();
    }

    // 获取指定ID的货架
    Shelf getShelfById(int shelfId) const {
        for (const auto &shelf : m_shelves) {
            if (shelf.get_id() == shelfId) {
                return shelf;
            }
        }
        return Shelf(); // 返回默认构造的空货架
    }

signals:
    void warehouseIdChanged(int id);
    void warehouseNameChanged(const QString &name);
    void warehouseQuantityChanged(int quantity);
    void shelvesChanged();

private:
    int m_warehouseId;                 // 仓库ID
    QString m_warehouseName;           // 仓库名称
    int m_warehouseQuantity;           // 仓库物料总数量
    QList<Shelf> m_shelves;            // 关联的货架列表（一对多关系）
};

#endif // WAREHOUSEVO_H
